﻿/*
* ibio-develop
* Tue Jan 15 14:19:05 2008
*/

import mx.events.EventDispatcher;
import utils.JSON;
import utils.Delegate;
class utils.JSONHelper{
	var m_lv:LoadVars;
	var m_obj:Object;
	var addEventListener:Function;
	var removeEventListener:Function;
	var dispatchEvent:Function;
	
	function JSONHelper(){
		EventDispatcher.initialize(this);
		m_lv = new LoadVars();
		m_lv.onData = Delegate.create(this, onVarsLoad);
		
	}
	
	function load(url){
		m_lv.load(url);
	}
	
	public static function objToStr(obj){
		try {
			var s:String = JSON.stringify(obj);
			return s;
		}catch(ex){
			trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text);
		}
	}
	
	public static function strToObj(str){
		try {
			var o:Object = JSON.parse(str);
			return o;
		}catch(ex){
			trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text);
		}
	}
	
	function get obj(){
		return m_obj;
	}
	
	/*
	* private functions
	*/
	private function onVarsLoad(str:String){
		if(str == undefined){
			return;
		}
		try {
			m_obj = JSON.parse(str);
		}catch(ex){
			trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text);
		}
		dispatchEvent({type:"onLoad", o:m_obj});
	}

}